package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.treepanel;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Node;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.MenuItem;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilConstants;
import edu.stanford.bmir.protege.web.client.ui.library.msgbox.MessageBox;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.GWTExtUtil;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.StkosPanelType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.treepanel.StkosClassTreePanel;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.menuitem.CancelCutterRecordMenuItem;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.menuitem.CancelCutterSubRecordMenuItem;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.menuitem.CutterRecordMenuItem;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.menuitem.CutterSubRecordMenuItem;
import edu.stanford.bmir.protege.web.shared.entity.OWLEntityData;

public class StkosCutterClassTreePanel extends StkosClassTreePanel {

	private static final StkosPanelType type = StkosPanelType.CLASS_TREE;

	private boolean showMapper;

	private Project accordingProject;

	public StkosCutterClassTreePanel(Project project) {
		this(project, false);
	}

	public StkosCutterClassTreePanel(Project project, boolean showMapper) {
		this(project, null, false);
	}

	public StkosCutterClassTreePanel(Project project, Project accordingProject,
			boolean showMapper) {
		super(project);
		this.accordingProject = accordingProject;
		this.showMapper = showMapper;
	}

	public void setAccordingProject(Project accordingProject) {
		this.accordingProject = accordingProject;
		onRefresh();
	}

	@Override
	protected void registerProjectEventHandler() {
	}

	@Override
	protected void onContextMenu(TreeNode node, EventObject e) {
		super.onContextMenu(node, e);
		if (node == null || getProject() == null) {
			MessageBox.showAlert("提示", "必要数据不完整，无法执行操作!");
			return;
		}

		String flagc = UtilConstants.FLAG_CUTTING + ":";
		boolean cutterRecordDisabled = true, cutterSubRecordDisabled = true, unCutterRecordDisabled = true, unCutterSubRecordDisabled = true;

		if ("Y".equals(node.getAttribute(flagc))) {
			unCutterRecordDisabled = false;
		} else {
			cutterRecordDisabled = false;
		}

		if (!node.isLeaf()) {
			cutterSubRecordDisabled = false;
			unCutterSubRecordDisabled = false;
		}

		Menu contextMenu = new Menu();

		MenuItem cutterRecord = new CutterRecordMenuItem(node, getProject(),
				this);
		cutterRecord.setDisabled(cutterRecordDisabled);
		contextMenu.addItem(cutterRecord);

		MenuItem cutterSubRecord = new CutterSubRecordMenuItem(node,
				getProject(), this);
		cutterSubRecord.setDisabled(cutterSubRecordDisabled);
		contextMenu.addItem(cutterSubRecord);

		contextMenu.addSeparator();

		MenuItem unCutterRecord = new CancelCutterRecordMenuItem(node,
				getProject(), this);
		unCutterRecord.setDisabled(unCutterRecordDisabled);
		contextMenu.addItem(unCutterRecord);

		MenuItem unCutterSubRecord = new CancelCutterSubRecordMenuItem(node,
				getProject(), this);
		unCutterSubRecord.setDisabled(unCutterSubRecordDisabled);
		contextMenu.addItem(unCutterSubRecord);

		contextMenu.showAt(e.getXY()[0] + 5, e.getXY()[1] + 5);
	}

	@Override
	protected TreeNode createTreeNode(EntityData entityData) {
		TreeNode node = super.createTreeNode(entityData);
		if (node == null) {
			return node;
		}
		if (showMapper && accordingProject != null) {
			addAnnotationMappingNoNodeMark(node, entityData, accordingProject
					.getProjectId().getId());
		}
		addAnnotationCuttingFlagNodeMark(node, entityData);
		return node;
	}

	public boolean isShowMapper() {
		return showMapper;
	}

	public void setShowMapper(boolean showMapper) {
		if (this.showMapper == showMapper) {
			return;
		}
		this.showMapper = showMapper;
		TreeNode root = this.getRootNode();
		loop(root);
	}

	protected void loop(TreeNode treeNode) {
		if (treeNode == null) {
			return;
		}
		if (isShowMapper()) {
			EntityData entityData = null;
			if (treeNode.getUserObject() instanceof OWLEntityData) {
				OWLEntityData oed = (OWLEntityData) treeNode.getUserObject();
				entityData = new EntityData(
						oed.getEntity().getIRI().toString(),
						oed.getBrowserText());
			} else if (treeNode.getUserObject() instanceof EntityData) {
				entityData = (EntityData) treeNode.getUserObject();
			} else {
			}
			if (entityData != null && accordingProject != null) {
				addAnnotationMappingNoNodeMark(treeNode, entityData,
						accordingProject.getProjectId().getId());
			}
		} else {
			Integer mapperNumber = getNodeMapperNumber(treeNode);
			String text = treeNode.getText();
			if (mapperNumber != null && text != null) {
				String mapperNumberLabel = GWTExtUtil
						.createNodeMapperNumberLabel(mapperNumber);
				text = text.replace(mapperNumberLabel, "");
				treeNode.setText(text);
			}
		}
		Node[] children = treeNode.getChildNodes();
		if (children != null && children.length > 0) {
			for (Node node : children) {
				loop((TreeNode) node);
			}
		}
	}
}
